package com.qfly.main.authentication.service;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Wisdytech.cn
 * com.qfly.main.authentication.service.RbacAuthorityService
 *
 * @author hailin.zhl
 * @date 2018/12/04
 */
@Component("rbacAuthorityService")
public class RbacAuthorityService {

    /**
     * 角色资源缓存，key 为角色，List<String> 为该角色拥有的资源url集合
     */
    public static final ConcurrentMap<String, List<String>> roleResources = new ConcurrentHashMap<>();

    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        /*Object userInfo = authentication.getPrincipal();
        boolean hasPermission = false;
        if (userInfo instanceof UserDetails) {
            String username = ((UserDetails) userInfo).getUsername();
            //获取资源
            Set<String> urls = new HashSet();
            urls.add("/common/inner"); // 这些 url 都是要登录后才能访问，且其他的 url 都不能访问！
            urls.add("/adduser");
            Set set2 = new HashSet();
            Set set3 = new HashSet();

            AntPathMatcher antPathMatcher = new AntPathMatcher();

            for (String url : urls) {
                if (antPathMatcher.match(url, request.getRequestURI())) {
                    hasPermission = true;
                    break;
                }
            }
            return hasPermission;
        } else {
            return false;
        }*/
        return true;
    }

    /**
     * 初始化map集合
     */
    private synchronized void initRoleResources() {

    }

    /**
     * 重置改角色拥有的资源
     * @param role
     */
    private synchronized void resetRoleResources(String role) {
        List<String> resources = roleResources.get("role");
    }
}
